package com.ysx.server.controller;

import com.ysx.server.pojo.dto.MessageDTO;
import com.ysx.server.pojo.vo.MessageVO;
import com.ysx.server.service.MessageParentService;
import com.ysx.server.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private MessageParentService messageParentService;

    @PostMapping("/save")
    public boolean saveMessage(@RequestBody MessageDTO messageDTO) {
        log.info("messageDTO:{}",messageDTO);
        if(messageDTO==null)
            return false;
        if(messageDTO.getIsParent()==1){
            messageDTO.setCreateName(messageDTO.getSendName());
            messageParentService.save(messageDTO.getSendName(),messageDTO.getGoodsId());
        }
        messageService.save(messageDTO);
        return true;
    }

    @GetMapping("/getMsg")
    public List<MessageVO> getMsg(Long userId){
        if(userId<=0)
            return null;
        List<MessageVO> messageVOList=messageService.getMsg(userId);
        log.info("messageVOList:{}",messageVOList);
        return messageVOList;
    }

    @GetMapping("/status/{parentId}")
    public boolean updateStatus(@PathVariable Long parentId){
        messageParentService.updateStatus(parentId);
        return true;
    }
}
